package com.xrd.uart.uart;

import com.xrd.serialport.SerialPort;
import com.xrd.uart.interaction.BaseReaderHelper;
import com.xrd.uart.interaction.ModuleConnector;

import java.io.File;

/**
 * Creatd by Yumiko
 * All is boring expect MeiZi.
 * on 2019/11/28
 * gradlew makeJar
 */
public class ReaderConnector implements ModuleConnector {

    private SerialPort mSeialPort = null;


    private BaseReaderHelper mReaderHelper;


    @Override
    public boolean connectCom(BaseReaderHelper helper, String port, int baud) {
        this.disConnect();
        return connectCom2(helper, port, baud);
    }

    @Override
    public boolean connectCom(BaseReaderHelper helper, int baud) {
        this.disConnect();
        String port;
        if (helper instanceof TtyS0ReaderHelper) {
            port = "dev/ttyS0";
        } else if (helper instanceof TtyS1ReaderHelper) {
            port = "dev/ttyS1";
        } else if
        (helper instanceof TtyS2ReaderHelper) {
            port = "dev/ttyS2";
        } else if
        (helper instanceof TtyS3ReaderHelper) {
            port = "dev/ttyS3";
        } else if
        (helper instanceof TtyS4ReaderHelper) {
            port = "dev/ttyS4";
        }else {
            return false;
        }
        return connectCom2(helper, port, baud);
    }


    private boolean connectCom2(BaseReaderHelper helper, String port, int baud) {
        try {
            if (helper == null) {
                throw new Exception("BaseReaderHelper are null exception!");
            }
            mReaderHelper = helper;
            this.mSeialPort = new SerialPort(new File(port), baud, 0);
            if (mSeialPort == null) {
                return false;
            }
        } catch (Exception var5) {
            var5.printStackTrace();
            return false;
        }
        try {
            mReaderHelper.setReader(this.mSeialPort.getInputStream(), this.mSeialPort.getOutputStream(), new ReaderDataPackageParser(), new ReaderDataPackageProcess());
            return true;
        } catch (Exception var4) {
            var4.printStackTrace();
            return false;
        }
    }


    private boolean connectCom1(String port, int baud) {
        try {
            this.mReaderHelper = TtyS2ReaderHelper.getDefaultHelper();
            this.mSeialPort = new SerialPort(new File(port), baud, 0);
            if (mSeialPort == null) {
                return false;
            }
        } catch (Exception var5) {
            var5.printStackTrace();
            return false;
        }
        try {
            this.mReaderHelper.setReader(this.mSeialPort.getInputStream(), this.mSeialPort.getOutputStream(), new ReaderDataPackageParser(), new ReaderDataPackageProcess());
            return true;
        } catch (Exception var4) {
            var4.printStackTrace();
            return false;
        }
    }

    @Override
    public void setTimeOut(long mill) {
        mReaderHelper.setTimeOut(mill);
    }

    @Override
    public void setLogShow(boolean isShow) {
        mReaderHelper.setLogIsShow(isShow);
    }

    @Override
    public void setEndFlag(byte endFlag) {
        mReaderHelper.setEndFlag(endFlag);
    }

    @Override
    public boolean connect(String var1, int var2) {
        return false;
    }

    @Override
    public boolean isConnected() {
        return this.mReaderHelper != null && this.mReaderHelper.isAlive();
    }

    @Override
    public void disConnect() {
        try {
            if (this.mReaderHelper != null) {
                this.mReaderHelper.signOut();
            }

            if (this.mSeialPort != null) {
                this.mSeialPort.close();
            }
        } catch (Exception var2) {
            var2.printStackTrace();
        }

        this.mSeialPort = null;
        this.mReaderHelper = null;
    }




}
